#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <sys/param.h>

#include <time.h>

#define PARENT /home/leza/Desktop/Code/Server/deamon/deamon


void daemon_init()
{
    /*
	创建守护进程
	*/
    int i;
    pid_t pid;

    pid = fork();
    if (pid < 0)
    {
        perror("fork error!");
        exit(1);
    }
    else if (pid > 0)
    {
        while (1) {
            sleep(1);
            fprintf(stdout, "%d\n", getpid());
        }
        exit(0);
    }
    setsid();

    char szPath[1024];
    if (getcwd(szPath, sizeof(szPath)) == NULL)
    {
        exit(1);
    }
    else
    {
        chdir(szPath);
    }

    umask(0);
    for (i = 0; i < 3; ++i)
    {
        close(i);
    }
    signal(SIGCHLD, SIG_IGN);

    //	printf("close stream....after \n"); //没用了
}

int main()
{
    time_t now;
    char buf[128] = {0};
    printf("pid = %d\n", getpid());

    daemon_init();

    system("echo \"TestDaemonProcess!\\n\" >> logspp");

    while (1)
    {
        sleep(1);
        
    }

    

    return 0;
}